package controller;

import core.BaseServlet;
import service.WorkerService;
import util.LogUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "IndexServlet", urlPatterns = "/index")
public class IndexServlet extends BaseServlet {

    /**
     * 首页页面
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void indexPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/pages/index.jsp").forward(request, response);
    }

    /**
     * 个人信息页面
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void loginInfoPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/pages/loginInfo.jsp").forward(request, response);
    }

    /**
     * 退出方法
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void loginOut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 清除保存的session
        LogUtil.addLog("退出登录管理系统", request);
        request.getSession().removeAttribute("worker");
        response.sendRedirect("/login?m=loginPage");
    }

    /**
     * 欢迎页面
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void welcome(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/pages/welcome.jsp").forward(request, response);
    }
    /**
     * 获取登录用户的权限
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void getPermission(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        WorkerService workerService = new WorkerService();
        workerService.getPermission(request, response);
    }
}
